package com.slz3.business.module.messageManagement.mesOrder.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.messageManagement.mesOrder.domain.MesOrder;
import com.slz3.business.module.messageManagement.mesOrder.domain.MesOrderFactory;
import com.slz3.business.module.messageManagement.mesOrder.steward.MesOrderSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class MesOrderServiceImpl implements MesOrderService {
    @Override
    public MesOrder save(MesOrder mesOrder) {
        return Repositorys.mesOrder.save(MesOrderFactory.initBaseFields(mesOrder));
    }
    @Override
    public Page<MesOrder> page(MultiValueMap<String, String> params) {
        Page<MesOrder> mesOrderPage = Repositorys.mesOrder.findAll(
                MesOrderSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return mesOrderPage;
    }


    @Override
    public MesOrder modify(MesOrder mesOrderNew) {
        return Repositorys.mesOrder.saveAndFlush(MesOrderFactory.initBaseFields(mesOrderNew));
    }

    @Override
    public Optional<MesOrder> single(UUID uuid) {
        return Repositorys.mesOrder.findById(uuid);
    }

    @Override
    public List<MesOrder> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.mesOrder.findAllById(uuids);
    }

    @Override
    public List<MesOrder> delete(List<MesOrder> mesOrderList) {
        mesOrderList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            MesOrderFactory.initBaseFields(item);
        });
        return Services.mesOrderTransactional.batchModify(mesOrderList);
    }

}
